package com.kmzx.one.project.controller;

import com.kmzx.one.common.utils.MyAssert;
import com.kmzx.one.project.net.BaseResult;
import com.kmzx.one.project.others.dto.NoticeInfo;
import com.kmzx.one.project.others.dto.UserMsgInfo;
import com.kmzx.one.project.service.SimpleService;
import com.kmzx.one.project.service.UserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Project one
 * @Author: carol
 * @Date: 2020/9/3-10:28
 */

@Api(tags = "快美甄选_[ 系统消息_接口 ]")
@RestController
public class UserMessageController extends BaseController {

    @Autowired
    UserMessageService userMessageService;

    @Autowired
    SimpleService simpleService;


    @ApiOperation(value = "1.检测用户是否有通知消息", notes = "1.检测用户是否有通知消息", position = -1)
    @GetMapping(value = "notice/has")
    public BaseResult checkUserMsgHas(String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            int has = userMessageService.checkUserMsgHas(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(has);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "2.用户的通知消息", notes = "2.用户的通知消息")
    @GetMapping(value = "notice/list")
    public BaseResult findUserMsgList(String userId, Integer pageNum, Integer pageSize) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            pageNum = pageNum == null ? super.pageNum : pageNum;
            pageSize = pageSize == null ? super.pageSize : pageSize;
            List<UserMsgInfo> info = userMessageService.findUserMsgInfo(userId, pageNum, pageSize);
            userMessageService.readMessageUserAll(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "3.读取通知消息", notes = "3.读取通知消息")
    @GetMapping(value = "notice/doRead")
    public BaseResult readingMsg(String userId, String id) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            userMessageService.readingUserMsg(userId, id);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(200);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "4.拉取通知 内容", notes = "4.拉取通知 内容")
    @GetMapping(value = "notice/pull")
    public BaseResult findNoticeInfo(String type) {

        try {
            List<NoticeInfo> info = simpleService.findNoticeInfoByType(type);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "5.拉取有关商品的通知", notes = "5.拉取有关商品的通知")
    @GetMapping(value = "notice/pullTwo")
    public BaseResult findNoticeInfoProdId(String type,String prodId) {
        try {
            List<NoticeInfo> info = simpleService.findNoticeByTwo(type,prodId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "6.确认通知 内容", notes = "6.确认通知 内容")
    @GetMapping(value = "notice/confirm")
    public BaseResult confirmNoticeInfo(String userId, String msgId) {
        try {
            simpleService.confirmNoticeUser(userId,msgId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(200);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


}
